''File: libtcomm
''Library to ease communicating between threads.

'libtcomm - Thread Communications library for FreeBASIC
'Copyright (C) 2013  Ebben Feagan <sir@mud.owlbox.net>
'
'This library is free software; you can redistribute it and/or
'modify it under the terms of the GNU Lesser General Public
'License as published by the Free Software Foundation; either
'version 2.1 of the License, or (at your option) any later version.
'
'This library is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'Lesser General Public License for more details.
'
'You should have received a copy of the GNU Lesser General Public
'License along with this library; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#ifndef __LIB_TCOMM_BI
#define __LIB_TCOMM_BI -1

#ifndef DLL
    #inclib "tcomm"
#endif

''Class: MData
''Simple class for managing data
type MData
    ''Variable: d
    ''Application specific pointer
    d as any ptr
    ''Variable: f
    ''Function to be called to free the memory for <d>
    f as sub( byval as any ptr )
    ''Sub: constructor
    ''
    ''Parameters:
    ''dt - pointer to set to d
    ''frefun - the free function to use
    declare constructor( byval dt as any ptr, byval frefun as sub( byval as any ptr ) )
end type


type ComChannel_f as CommChannel

''Class: Message
type Message

    ''Sub: constructor
    ''Used for creating Messages
    ''
    ''Parameters:
    ''cm - the "command" to pass, this is application dependant
    ''xtra - optional pointer to newly allocated <MData> object (will be managed by Message object)
    ''cc - optional pointer to a <CommChannel> to use for communicating
    declare constructor( byval cm as integer, byval xtra as Mdata ptr = 0, byval cc as ComChannel_f ptr =0)
    declare destructor()
    c as integer
    x as MData ptr
    m as ComChannel_f ptr
end type

type Messages
    d as Message ptr
    n as Messages ptr
    declare constructor( byval rhs as Message ptr )
    declare destructor()
end type

''Class: CommChannel
''Used for asynchronus communication across threads
type CommChannel
    public:
        declare constructor()
        declare destructor()
        ''Sub: send
        ''Send a message
        ''
        ''Parameters:
        ''m - <Message> ptr to send, do not free
        declare sub send( byval m as Message ptr )

        ''Function: recv
        ''Check for any messages and return the oldest one.
        ''
        ''Returns:
        ''NULL if no <Messages> awaiting, or a <Message> ptr
        ''that you must free when done with it using delete.
        declare function recv( ) as Message ptr
    private:
        _mess as Messages ptr
        _mutex as any ptr
end type

#endif
